﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Xml.Linq;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Resources.Core;
using Windows.Data.Xml.Dom;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Xml;
using System.Threading;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
namespace MultiApp.UserControls
{
    public sealed partial class TimezoneControl : UserControl
    {
        Windows.ApplicationModel.Resources.ResourceLoader loader = new Windows.ApplicationModel.Resources.ResourceLoader("Strings");
        public TimezoneControl()
        {
            this.InitializeComponent();
            SetText();
            txtInput.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
            TimeZones();
            
        }

        private void SetText()
        {
            lblTime.Text = loader.GetString("lblTime");
            lblTimeZone.Text = loader.GetString("lblTimeZone");
            btnConvert.Content = loader.GetString("btnConvert");
        }

        public async void TimeZones()
        {
            
           StorageFile doc;
            if(DateTime.Now.IsDaylightSavingTime())
            {
                doc = await Package.Current.InstalledLocation.GetFileAsync("Data\\summerTime.txt"); 
            }
            else
            {
                doc = await Package.Current.InstalledLocation.GetFileAsync("Data\\winterTime.txt"); 
            }
            
            var time = await FileIO.ReadLinesAsync(doc);
            comboInput.ItemsSource = time;


            
            
        }

        private void btnConvert_Click(object sender, RoutedEventArgs e)
        {
            
            if (comboInput.SelectedItem == null)
            {

                lblError.Text =loader.GetString("TimeError");
                lblError.Foreground=new SolidColorBrush(Colors.Red);
            }
            else
            {


                var timeTest = new DateTime();
                var input = txtInput.Text;
                var fInput = new DateTime();
                try
                {
                    fInput = DateTime.ParseExact(input, "dd/MM/yyyy HH:mm",
                                                 System.Globalization.CultureInfo.InvariantCulture);
                }
                catch (Exception)
                {
                    fInput = DateTime.ParseExact(input, "dd-MM-yyyy HH:mm",
                                                 System.Globalization.CultureInfo.InvariantCulture);
                }
                var selectedFrom = "";
                try
                {
                    selectedFrom = comboInput.SelectedItem.ToString().Substring(4, 7);
                }
                catch (Exception)
                {   


                }
                var t = selectedFrom;
                if (t.Contains("00:00"))
                {
                    t = t.Replace("+", "");
                    t = t.Replace(")", "");
                    t = t.Replace(":00", "");
                    timeTest = TimeZoneInfo.ConvertTime(fInput, TimeZoneInfo.Utc) +
                               new TimeSpan(0, Convert.ToInt32(t), 0, 0);
                }
                else
                {


                    t = selectedFrom.Replace("00", "");
                    if (selectedFrom.Contains("+"))
                    {
                        t = t.Replace("+", "");
                        t = t.Replace(")", "");
                        t = t.Replace(":", "");
                        timeTest = TimeZoneInfo.ConvertTime(fInput, TimeZoneInfo.Utc) +
                                   new TimeSpan(0, Convert.ToInt32(t), 0, 0);
                    }
                    else
                    {
                        t = t.Replace("-", "");
                        t = t.Replace(")", "");
                        t = t.Replace(":", "");
                        timeTest = TimeZoneInfo.ConvertTime(fInput, TimeZoneInfo.Utc) +
                                   new TimeSpan(0, -Convert.ToInt32(t), 0, 0);
                    }
                }

                lblError.Text = "";
                txtResult.Text = timeTest.ToString("dd/MM/yyyy HH:mm");
            }

        }
    }
}
